<template>
  <div class="advanced-loading" :class="{ 'visible': visible }">
    <div class="loading-container">
      <!-- DNA 螺旋动画 -->
      <div class="dna-loader" v-if="type === 'dna'">
        <div class="dna-strand">
          <div class="dna-base" v-for="i in 8" :key="i" :style="{ animationDelay: i * 0.1 + 's' }"></div>
        </div>
        <div class="dna-strand">
          <div class="dna-base" v-for="i in 8" :key="i" :style="{ animationDelay: (i * 0.1 + 0.5) + 's' }"></div>
        </div>
      </div>

      <!-- 代码编译动画 -->
      <div class="code-loader" v-if="type === 'code'">
        <div class="code-lines">
          <div class="code-line" v-for="i in 6" :key="i" :style="{ animationDelay: i * 0.2 + 's' }">
            <span class="code-text">{{ getCodeLine(i) }}</span>
            <span class="cursor">|</span>
          </div>
        </div>
      </div>

      <!-- 神经网络动画 -->
      <div class="neural-loader" v-if="type === 'neural'" ref="neuralContainer"></div>

      <!-- 几何变换动画 -->
      <div class="geometry-loader" v-if="type === 'geometry'">
        <div class="geometry-shape"></div>
      </div>

      <!-- 加载文字 -->
      <div class="loading-text">
        <h2 class="loading-title">{{ title }}</h2>
        <p class="loading-subtitle">{{ subtitle }}</p>
        <div class="loading-progress">
          <div class="progress-bar">
            <div class="progress-fill" :style="{ width: progress + '%' }"></div>
          </div>
          <span class="progress-text">{{ progress }}%</span>
        </div>
      </div>
    </div>

    <!-- 粒子背景 -->
    <div class="particles-bg" ref="particlesContainer"></div>
  </div>
</template>

<script setup>
import { ref, onMounted, onUnmounted, watch } from 'vue'
import { gsap } from 'gsap'

// Props
const props = defineProps({
  visible: {
    type: Boolean,
    default: true
  },
  type: {
    type: String,
    default: 'dna', // dna, code, neural, geometry
    validator: (value) => ['dna', 'code', 'neural', 'geometry'].includes(value)
  },
  title: {
    type: String,
    default: '加载中...'
  },
  subtitle: {
    type: String,
    default: '正在为您准备精彩内容'
  },
  duration: {
    type: Number,
    default: 3000 // 加载持续时间（毫秒）
  }
})

// Emits
const emit = defineEmits(['complete'])

// 响应式数据
const progress = ref(0)
const neuralContainer = ref(null)
const particlesContainer = ref(null)

// 代码行文本
const codeLines = [
  'import { magic } from "universe"',
  'const reality = new Reality()',
  'reality.render(awesome)',
  'console.log("Loading...")',
  'await fetchContent()',
  'export default Experience'
]

// 获取代码行
function getCodeLine(index) {
  return codeLines[index - 1] || ''
}

// 创建粒子背景
function createParticles() {
  if (!particlesContainer.value) return

  const particleCount = 50
  for (let i = 0; i < particleCount; i++) {
    const particle = document.createElement('div')
    particle.className = 'particle'
    particle.style.left = Math.random() * 100 + '%'
    particle.style.top = Math.random() * 100 + '%'
    particle.style.animationDelay = Math.random() * 3 + 's'
    particle.style.animationDuration = (3 + Math.random() * 2) + 's'
    particlesContainer.value.appendChild(particle)
  }
}

// 创建神经网络动画
function createNeuralNetwork() {
  if (!neuralContainer.value) return

  // 节点
  const nodes = []
  for (let i = 0; i < 12; i++) {
    const node = document.createElement('div')
    node.className = 'neural-node'
    node.style.left = Math.random() * 80 + 10 + '%'
    node.style.top = Math.random() * 80 + 10 + '%'
    neuralContainer.value.appendChild(node)
    nodes.push(node)
  }

  // 连接线
  for (let i = 0; i < nodes.length; i++) {
    for (let j = i + 1; j < nodes.length; j++) {
      if (Math.random() > 0.7) {
        const line = document.createElement('div')
        line.className = 'neural-connection'
        
        const rect1 = nodes[i].getBoundingClientRect()
        const rect2 = nodes[j].getBoundingClientRect()
        const containerRect = neuralContainer.value.getBoundingClientRect()
        
        const x1 = (rect1.left + rect1.width/2 - containerRect.left) / containerRect.width * 100
        const y1 = (rect1.top + rect1.height/2 - containerRect.top) / containerRect.height * 100
        const x2 = (rect2.left + rect2.width/2 - containerRect.left) / containerRect.width * 100
        const y2 = (rect2.top + rect2.height/2 - containerRect.top) / containerRect.height * 100
        
        const length = Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2))
        const angle = Math.atan2(y2 - y1, x2 - x1) * 180 / Math.PI
        
        line.style.width = length + '%'
        line.style.left = x1 + '%'
        line.style.top = y1 + '%'
        line.style.transform = `rotate(${angle}deg)`
        line.style.transformOrigin = '0 50%'
        
        neuralContainer.value.appendChild(line)
      }
    }
  }
}

// 开始加载动画
function startLoading() {
  // 进度条动画
  gsap.to(progress, {
    value: 100,
    duration: props.duration / 1000,
    ease: "power2.out",
    onComplete: () => {
      emit('complete')
    }
  })

  // 文字动画
  gsap.fromTo('.loading-title', 
    { opacity: 0, y: 30 },
    { opacity: 1, y: 0, duration: 1, ease: "power3.out" }
  )
  
  gsap.fromTo('.loading-subtitle',
    { opacity: 0, y: 20 },
    { opacity: 1, y: 0, duration: 1, delay: 0.3, ease: "power3.out" }
  )
}

// 监听可见性变化
watch(() => props.visible, (newVal) => {
  if (newVal) {
    startLoading()
  }
})

// 组件挂载
onMounted(() => {
  createParticles()
  
  if (props.type === 'neural') {
    setTimeout(createNeuralNetwork, 100)
  }
  
  if (props.visible) {
    startLoading()
  }
})
</script>

<style scoped>
.advanced-loading {
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
  background: radial-gradient(circle at 50% 50%, #1a1a2e, #16213e, #0f0f23);
  z-index: 9999;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.5s ease;
}

.advanced-loading.visible {
  opacity: 1;
  pointer-events: all;
}

.loading-container {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  text-align: center;
  z-index: 10;
}

/* DNA 螺旋动画 */
.dna-loader {
  position: relative;
  width: 100px;
  height: 200px;
  margin: 0 auto 2rem;
}

.dna-strand {
  position: absolute;
  width: 4px;
  height: 100%;
  left: 50%;
  transform: translateX(-50%);
}

.dna-strand:nth-child(2) {
  transform: translateX(-50%) rotateY(180deg);
}

.dna-base {
  position: absolute;
  width: 40px;
  height: 4px;
  background: linear-gradient(45deg, #00ffff, #ff00ff);
  left: 50%;
  transform: translateX(-50%);
  animation: dnaRotate 2s infinite linear;
}

.dna-base:nth-child(odd) {
  background: linear-gradient(45deg, #ff00ff, #ffff00);
}

@keyframes dnaRotate {
  0% { transform: translateX(-50%) rotateZ(0deg) translateX(20px); }
  100% { transform: translateX(-50%) rotateZ(360deg) translateX(20px); }
}

/* 代码编译动画 */
.code-loader {
  width: 400px;
  margin: 0 auto 2rem;
  background: rgba(0, 0, 0, 0.8);
  border-radius: 8px;
  padding: 1rem;
  border: 1px solid #333;
}

.code-lines {
  text-align: left;
  font-family: 'Courier New', monospace;
}

.code-line {
  display: flex;
  align-items: center;
  margin: 0.5rem 0;
  opacity: 0;
  animation: typeIn 0.5s forwards;
}

.code-text {
  color: #00ff00;
  font-size: 0.9rem;
}

.cursor {
  color: #00ff00;
  animation: blink 1s infinite;
  margin-left: 2px;
}

@keyframes typeIn {
  to { opacity: 1; }
}

@keyframes blink {
  0%, 50% { opacity: 1; }
  51%, 100% { opacity: 0; }
}

/* 神经网络动画 */
.neural-loader {
  position: relative;
  width: 300px;
  height: 200px;
  margin: 0 auto 2rem;
}

.neural-node {
  position: absolute;
  width: 12px;
  height: 12px;
  background: radial-gradient(circle, #00ffff, #0088ff);
  border-radius: 50%;
  animation: neuralPulse 2s infinite;
  box-shadow: 0 0 10px #00ffff;
}

.neural-connection {
  position: absolute;
  height: 2px;
  background: linear-gradient(90deg, transparent, #00ffff, transparent);
  animation: neuralFlow 3s infinite;
}

@keyframes neuralPulse {
  0%, 100% { transform: scale(1); opacity: 0.7; }
  50% { transform: scale(1.5); opacity: 1; }
}

@keyframes neuralFlow {
  0% { opacity: 0; }
  50% { opacity: 0.8; }
  100% { opacity: 0; }
}

/* 几何变换动画 */
.geometry-loader {
  margin: 0 auto 2rem;
}

.geometry-shape {
  width: 100px;
  height: 100px;
  margin: 0 auto;
  background: linear-gradient(45deg, #ff6b6b, #4ecdc4, #45b7d1, #96ceb4);
  background-size: 400% 400%;
  animation: geometryMorph 3s infinite, gradientShift 4s infinite;
  clip-path: polygon(50% 0%, 0% 100%, 100% 100%);
}

@keyframes geometryMorph {
  0% { clip-path: polygon(50% 0%, 0% 100%, 100% 100%); transform: rotate(0deg); }
  25% { clip-path: polygon(0% 0%, 0% 100%, 100% 100%, 100% 0%); transform: rotate(90deg); }
  50% { clip-path: polygon(30% 0%, 0% 70%, 70% 100%, 100% 30%); transform: rotate(180deg); }
  75% { clip-path: polygon(50% 0%, 100% 50%, 50% 100%, 0% 50%); transform: rotate(270deg); }
  100% { clip-path: polygon(50% 0%, 0% 100%, 100% 100%); transform: rotate(360deg); }
}

@keyframes gradientShift {
  0% { background-position: 0% 50%; }
  50% { background-position: 100% 50%; }
  100% { background-position: 0% 50%; }
}

/* 加载文字 */
.loading-text {
  color: white;
}

.loading-title {
  font-size: 2.5rem;
  font-weight: bold;
  margin: 0 0 0.5rem 0;
  background: linear-gradient(45deg, #00ffff, #ff00ff, #ffff00);
  background-size: 200% 200%;
  background-clip: text;
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  animation: textGradient 2s infinite;
}

.loading-subtitle {
  font-size: 1.2rem;
  color: #cccccc;
  margin: 0 0 2rem 0;
}

.loading-progress {
  display: flex;
  align-items: center;
  gap: 1rem;
  justify-content: center;
}

.progress-bar {
  width: 200px;
  height: 8px;
  background: rgba(255, 255, 255, 0.2);
  border-radius: 4px;
  overflow: hidden;
}

.progress-fill {
  height: 100%;
  background: linear-gradient(90deg, #00ffff, #ff00ff);
  border-radius: 4px;
  transition: width 0.3s ease;
}

.progress-text {
  color: #00ffff;
  font-weight: bold;
  min-width: 40px;
}

@keyframes textGradient {
  0% { background-position: 0% 50%; }
  50% { background-position: 100% 50%; }
  100% { background-position: 0% 50%; }
}

/* 粒子背景 */
.particles-bg {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
  z-index: 1;
}

.particle {
  position: absolute;
  width: 4px;
  height: 4px;
  background: radial-gradient(circle, #00ffff, transparent);
  border-radius: 50%;
  animation: particleFloat 5s infinite linear;
}

@keyframes particleFloat {
  0% {
    transform: translateY(100vh) rotate(0deg);
    opacity: 0;
  }
  10% {
    opacity: 1;
  }
  90% {
    opacity: 1;
  }
  100% {
    transform: translateY(-10vh) rotate(360deg);
    opacity: 0;
  }
}

/* 响应式设计 */
@media (max-width: 768px) {
  .loading-title {
    font-size: 2rem;
  }
  
  .loading-subtitle {
    font-size: 1rem;
  }
  
  .code-loader {
    width: 300px;
  }
  
  .progress-bar {
    width: 150px;
  }
}
</style>
